package com.jingzhi.huimiao.mineActivity;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jingzhi.huimiao.MainActivity;
import com.jingzhi.huimiao.R;
import com.jingzhi.huimiao.bean.RankingInfo;
import com.jingzhi.huimiao.bean.User;
import com.jingzhi.huimiao.utils.CanstantUtils;
import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.HttpUtils;
import com.lidroid.xutils.BitmapUtils;

public class RankingActivity extends Activity {

	private ImageView iv_mine_pic;
	private LinearLayout iv_rank_back;
	private TextView tv_nickname;
	private TextView tv_pank;
	private TextView tv_studytime;
	private ListView mine_lv;
	private RankingInfo result;

	private BitmapUtils bitmaputils;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ranking);
		
		initview();
		bitmaputils = new BitmapUtils(RankingActivity.this);
		long uid = DataStoreUtil.getLong(RankingActivity.this,"userid");
		// 将用户信息存放在Map中
					Map<String, String> userinfo = new HashMap<String, String>();
					userinfo.put("id", uid+"");
					try {
						String retStr = HttpUtils.sendPostRequest(
								CanstantUtils.rank_url, userinfo, "utf-8");
//						 Log.e("TAG------retStr", retStr);
						Gson gson = new Gson();
						result = gson.fromJson(retStr, RankingInfo.class);
					}catch (Exception e) {
						e.printStackTrace();
					}
		
				
					if(result!=null){
//						BitmapUtils bitmaputils = new BitmapUtils(RankingActivity.this);
//						bitmaputils.display(iv_mine_pic, result.getUser().getImagePath());
//						bitmaputils.closeCache();
						tv_nickname.setText(result.getUser().getNickname());
						tv_pank.setText("第"+result.getRanking()+"名");
						tv_studytime.setText("学习天数: "+result.getUser().getTotalDay()+"天");
						mine_lv.setAdapter(new MineAdapter());			
					}else{
						Toast.makeText(RankingActivity.this, "获取数据失败.....", 0).show();
					}
					
					iv_rank_back.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
//							startActivity(new Intent(RankingActivity.this,MainActivity.class));
//							StrictMode.setVmPolicy (new StrictMode.VmPolicy.Builder().detectAll().penaltyLog()
//                                    .penaltyDeath().build());
							bitmaputils.closeCache();
							finish();
						}
					});
	}

	class MineAdapter extends BaseAdapter{

		

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return result.getRankingUsers().length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHoder holder;
			if(convertView==null){
				holder = new ViewHoder();
				convertView = View.inflate(RankingActivity.this, R.layout.mine_lv_item, null);
				holder.iv_mine_head2 = (ImageView) convertView.findViewById(R.id.iv_rank_hp);
				holder.tv_rank_position = (TextView) convertView.findViewById(R.id.tv_rank_position);
				holder.tv_rank_name = (TextView) convertView.findViewById(R.id.tv_rank_name);
				holder.tv_rank_time = (TextView) convertView.findViewById(R.id.tv_rank_time);
				holder.tv_rank_exp = (TextView) convertView.findViewById(R.id.tv_rank_exp);
				convertView.setTag(holder);
			}else{
				holder = (ViewHoder) convertView.getTag();
			}
			
			
			bitmaputils.display(holder.iv_mine_head2, result.getRankingUsers()[position].getImagePath());
//			bitmaputils.closeCache();
//			bitmaputils.clearCache();
			holder.tv_rank_position.setText(position+1+"");
			
//			Log.e("资源；；；；；", result.getRankingUsers()[position].getNickname());
			holder.tv_rank_name.setText(result.getRankingUsers()[position].getNickname()+"");
			holder.tv_rank_time.setText(result.getRankingUsers()[position].getTotalDay()+"天");
			holder.tv_rank_exp.setText(result.getRankingUsers()[position].getExperience()+"");
			return convertView;
		}
		
		class ViewHoder{
			private ImageView iv_mine_head2;
			private TextView tv_rank_position;//排名
			private TextView tv_rank_name;//昵称
			private TextView tv_rank_time;//学习时间
			private TextView tv_rank_exp;//经验值
//			private ImageView iv_mine_head2;
		}
		
		
	}
	
	private void initview() {
		iv_mine_pic = (ImageView) findViewById(R.id.iv_mine_pic);
		tv_nickname = (TextView) findViewById(R.id.tv_nickname);
		tv_pank = (TextView) findViewById(R.id.tv_pank);
		tv_studytime = (TextView) findViewById(R.id.tv_studytime);
		mine_lv = (ListView) findViewById(R.id.mine_lv);
		iv_rank_back = (LinearLayout) findViewById(R.id.iv_rank_back);
	}
	
	
}
